
var flag=0;
//根据设计id去查对应的编码设计
function encodeProtocol(designId) {
    // $('#encodeField').dialog('open');

    $.ajax({
        url:HOST.concat("/protocolEncode/id?id="+designId+"&token=" + config.token),
        type:"GET",
        async:false,
        dataType:"json",
        success:function(res){
            console.log(res);
            if(res.meta.state=="success"){
                flag=0;
                //查询成功
                var keys=res.data;
                $("#designId").val(keys.designId);
                $("#eBody").empty();

                //认证协商编码格式设计
               if(keys.identityType_key!=""){

                    var div01Html='<div class="ML20">\
                                            <p id="encodeBtn2"><span class="glyphicon glyphicon-chevron-right"></span> 认证协商编码格式设计</p>\
                                            <div class="ML25" id="encodeDiv1">\
                                                <p id="encodeIdentity" class="encodeKey">'+keys.identityType_key+': </p>\
                                                <select id="identityType_val">\
                                                      <option value="0" id="identityType_0">0</option>\
                                                      <option value="1" id="identityType_1">1</option>\
                                                </select>\
                                            </div>\
                                    </div>';
                    $("#eBody").append(div01Html);
                    if(keys.identityType_val=="1"){
                        $("#identityType_1").attr("selected","selected");
                    }
                }


               //加密编码设计
                if(keys.encodeEncryAlg_key!=""||keys.encodeEncryModel_key!=""||keys.encodeOutput_key!=""||keys.encodeEncryArea_key!=""){

                    var tempDiv01Html='<div class="ML20" id="div02">\
                        <p id="encodeBtn3"><span class="glyphicon glyphicon-chevron-right"></span> 加密编码设计</p>\
                        <div class="ML25" id="encodeEncryDiv"></div>\
                        </div>';

                    $("#eBody").append(tempDiv01Html);

                    if(keys.encodeEncryAlg_key!=""){
                        //加密算法位
                        var tempDiv02Html='<p id="encodeBtn4"><span class="glyphicon glyphicon-chevron-right"></span> 加密算法位</p>\
                            <div class="ML25" id="encodeDiv2">\
                            <p id="encodeEncryAlg" class="encodeKey">'+keys.encodeEncryAlg_key+': </p>\
                            <select id="encodeEncryAlg_val">\
                            <option value="0" id="encodeEncryAlg_0">0</option>\
                            <option value="1" id="encodeEncryAlg_1">1</option>\
                            </select>\
                            </div>';
                        $("#encodeEncryDiv").append(tempDiv02Html);
                        if(keys.encodeEncryAlg_val=="1"){
                            $("#encodeEncryAlg_1").attr("selected","selected");
                        }
                    }

                    if(keys.encodeEncryModel_key!=""){
                        //加密模式位
                        var tempDiv03Html='<p id="encodeBtn5"><span class="glyphicon glyphicon-chevron-right"></span> 加密模式位</p>\
                            <div class="ML25" id="encodeDiv3">\
                            <p id="encodeEncryModel" class="encodeKey">'+keys.encodeEncryModel_key+': </p>\
                            <select id="encodeEncryModel_val">\
                            <option value="0" id="encodeEncryModel_0">0</option>\
                            <option value="1" id="encodeEncryModel_1">1</option>\
                            </select>\
                            </div>';

                        $("#encodeEncryDiv").append(tempDiv03Html);
                        if(keys.encodeEncryModel_val=="1"){
                            $("#encodeEncryModel_1").attr("selected","selected");
                        }
                    }

                    if(keys.encodeOutput_key!=""){
                        //输出格式位
                        var tempDiv04Html='<p id="encodeBtn6"><span class="glyphicon glyphicon-chevron-right"></span> 输出格式位</p>\
                            <div class="ML25" id="encodeDiv4">\
                            <p id="encodeOutput" class="encodeKey">'+keys.encodeOutput_key+': </p>\
                            <select id="encodeOutput_val">\
                            <option value="0" id="encodeOutput_0">0</option>\
                            <option value="1" id="encodeOutput_1">1</option>\
                            </select>\
                            </div>';

                        $("#encodeEncryDiv").append(tempDiv04Html);

                        if(keys.encodeOutput_val=="1"){
                            $("#encodeOutput_1").attr("selected","selected");
                        }
                    }

                    if(keys.encodeEncryArea_key!=""){
                        //加密部分位
                        var tempDiv05Html='<p id="encodeBtn7"><span class="glyphicon glyphicon-chevron-right"></span> 加密部分位</p>\
                            <div class="ML25" id="encodeDiv5">\
                            <p id="encodeEncryArea" class="encodeKey">'+keys.encodeEncryArea_key+': </p>\
                             <select id="encodeEncryArea_val">\
                            <option value="0" id="encodeEncryArea_0">0</option>\
                            <option value="1" id="encodeEncryArea_1">1</option>\
                            </select>\
                            </div>';

                        $("#encodeEncryDiv").append(tempDiv05Html);
                        if(keys.encodeEncryArea_val=="1"){
                            $("#encodeEncryArea_1").attr("selected","selected");
                        }

                    }

                }

                //异常检测编码设计
                if(keys.encodeException_key!=""){

                    var tempDiv06Html=' <div class="ML20" id="div03">\
                        <p id="encodeBtn8"><span class="glyphicon glyphicon-chevron-right"></span> 异常检测编码设计</p>\
                         <div class="ML25" id="encodeDiv6">\
                        <p id="encodeException" class="encodeKey">'+keys.encodeException_key+': </p>\
                        <select id="encodeException_val">\
                            <option value="0" id="encodeException_0">0</option>\
                            <option value="1" id="encodeException_1">1</option>\
                        </select>\
                        </div>\
                        </div>';

                    $("#eBody").append(tempDiv06Html);
                    if(keys.encodeException_val=="1"){
                        $("#encodeException_1").attr("selected","selected");
                    }
                }

                //完整性
                if(keys.encodeComplete_key!=""){
                    var tempDiv07Html=' <div class="ML20" id="div04">\
                        <p id="encodeBtn9"><span class="glyphicon glyphicon-chevron-right"></span> 完整性校验编码格式设计</p>\
                        <div class="ML25" id="encodeDiv7">\
                        <p id="encodeComplete" class="encodeKey">'+keys.encodeComplete_key+': </p> \
                        <select id="encodeComplete_val">\
                        <option value="0" id="encodeComplete_0">0</option>\
                        <option value="1" id="encodeComplete_1">1</option>\
                        </select>\
                        </div>\
                        </div>';

                    $("#eBody").append(tempDiv07Html);
                    if(keys.encodeComplete_val=="1"){
                        $("#encodeComplete_1").attr("selected","selected");
                    }

                }

                //防病毒编码格式设计
                if(keys.encodeVirus_key!=""){
                    var tempDiv08Html='<div class="ML20" id="div05">\
                        <p id="encodeBtn10"><span class="glyphicon glyphicon-chevron-right"></span> 防病毒编码格式设计</p>\
                        <div class="ML25" id="encodeDiv8">\
                        <p id="encodeVirus" class="encodeKey">'+keys.encodeVirus_key+': </p>\
                        <select id="encodeVirus_val">\
                        <option value="0" id="encodeVirus_0">0</option>\
                        <option value="1" id="encodeVirus_1">1</option>\
                        </select>\
                        </div>\
                        </div>';

                    $("#eBody").append(tempDiv08Html);
                    if(keys.encodeVirus_val=="1"){
                        $("#encodeVirus_1").attr("selected","selected");
                    }
                }

            }else{
                flag=1;
                alert("请先创建协议配置");
                var address="createConfiguration.html?id="+designId;
                window.location.href = address;
            }

        },
        failed:function(){
            flag=1;
            console.log("获取编码设计详情失败……");
        }

    });




    var w=window.screen.width;
    var h=window.screen.height;


    if(h>1030){
        h=h/10;
    }else{
        h=h/10+20;
    }

    if(w>1600){
        w=w/3+50
    }else{
        w=w/3;
    }
    $('#encodeField').dialog({
        //title: 'My Dialog',
        top: h,
        left:w,
        closed: false,
        cache: false,
        //href: 'get_content.php',
        //modal: true,
        onClose:function(){
            $("#encodeBtn1 span").attr("class", "glyphicon glyphicon-chevron-right");
            $("#eBody").empty();
        }
    });


    if(flag==1||parseInt(flag)==1||flag=="1"){

        $("#encodeField").dialog({
           closed:true
        });
    }

}



$("#encodeBtn1").live("click",function () {
    if ($("#encodeBtn1 span").attr("class") == "glyphicon glyphicon-chevron-right") {

        $("#encodeBtn1 span").attr("class", "glyphicon glyphicon-chevron-down");
        $(".ML20").css("display", "block");
    } else {

        $("#encodeBtn1 span").attr("class", "glyphicon glyphicon-chevron-right");
        $(".ML20").css("display", "none");
    }
});


$("#encodeBtn2").live("click",function () {
    if ($("#encodeBtn2 span").attr("class") == "glyphicon glyphicon-chevron-right") {
        $("#encodeBtn2 span").attr("class", "glyphicon glyphicon-chevron-down");
        $("#encodeDiv1").css("display","block");

    }else{
        $("#encodeBtn2 span").attr("class", "glyphicon glyphicon-chevron-right");
        $("#encodeDiv1").css("display","none");
    }
});


$("#encodeBtn3").live("click",function () {
    if ($("#encodeBtn3 span").attr("class") == "glyphicon glyphicon-chevron-right") {

        $("#encodeBtn3 span").attr("class", "glyphicon glyphicon-chevron-down");
        $("#encodeEncryDiv").css("display","block");


    }else{
        $("#encodeBtn3 span").attr("class", "glyphicon glyphicon-chevron-right");
        $("#encodeEncryDiv").css("display","none");

    }
});


$("#encodeBtn4").live("click",function () {
    if ($("#encodeBtn4 span").attr("class") == "glyphicon glyphicon-chevron-right") {

        $("#encodeBtn4 span").attr("class", "glyphicon glyphicon-chevron-down");
        $("#encodeDiv2").css("display","block");


    }else{
        $("#encodeBtn4 span").attr("class", "glyphicon glyphicon-chevron-right");
        $("#encodeDiv2").css("display","none");

    }
});


$("#encodeBtn5").live("click",function () {
    if ($("#encodeBtn5 span").attr("class") == "glyphicon glyphicon-chevron-right") {

        $("#encodeBtn5 span").attr("class", "glyphicon glyphicon-chevron-down");
        $("#encodeDiv3").css("display","block");


    }else{
        $("#encodeBtn5 span").attr("class", "glyphicon glyphicon-chevron-right");
        $("#encodeDiv3").css("display","none");

    }
});


$("#encodeBtn6").live("click",function () {
    if ($("#encodeBtn6 span").attr("class") == "glyphicon glyphicon-chevron-right") {

        $("#encodeBtn6 span").attr("class", "glyphicon glyphicon-chevron-down");
        $("#encodeDiv4").css("display","block");


    }else{
        $("#encodeBtn6 span").attr("class", "glyphicon glyphicon-chevron-right");
        $("#encodeDiv4").css("display","none");

    }
});


$("#encodeBtn7").live("click",function () {
    if ($("#encodeBtn7 span").attr("class") == "glyphicon glyphicon-chevron-right") {

        $("#encodeBtn7 span").attr("class", "glyphicon glyphicon-chevron-down");
        $("#encodeDiv5").css("display","block");


    }else{
        $("#encodeBtn7 span").attr("class", "glyphicon glyphicon-chevron-right");
        $("#encodeDiv5").css("display","none");

    }
});


$("#encodeBtn8").live("click",function () {
    if ($("#encodeBtn8 span").attr("class") == "glyphicon glyphicon-chevron-right") {

        $("#encodeBtn8 span").attr("class", "glyphicon glyphicon-chevron-down");
        $("#encodeDiv6").css("display","block");


    }else{
        $("#encodeBtn8 span").attr("class", "glyphicon glyphicon-chevron-right");
        $("#encodeDiv6").css("display","none");

    }
});

$("#encodeBtn9").live("click",function () {
    if ($("#encodeBtn9 span").attr("class") == "glyphicon glyphicon-chevron-right") {

        $("#encodeBtn9 span").attr("class", "glyphicon glyphicon-chevron-down");
        $("#encodeDiv7").css("display","block");


    }else{
        $("#encodeBtn9 span").attr("class", "glyphicon glyphicon-chevron-right");
        $("#encodeDiv7").css("display","none");

    }
});


$("#encodeBtn10").live("click",function () {
    if ($("#encodeBtn10 span").attr("class") == "glyphicon glyphicon-chevron-right") {

        $("#encodeBtn10 span").attr("class", "glyphicon glyphicon-chevron-down");
        $("#encodeDiv8").css("display","block");


    }else{
        $("#encodeBtn10 span").attr("class", "glyphicon glyphicon-chevron-right");
        $("#encodeDiv8").css("display","none");

    }
});

//保存修改协议编码设计信息
$("#encodeSave").live("click",function(){

    var protocolEncodeJson={
    //认证编码设计
    //identityType_key:$("#encodeIdentity").text().trim().replace(":",""),
    identityType_val:$("#identityType_val option:selected").val(),
    //加密编码设计
    //加密算法位

    //encodeEncryAlg_key:$("#encodeEncryAlg").text().trim().replace(":",""),
    encodeEncryAlg_val:$("#encodeEncryAlg_val option:selected").val(),

    //加密模式位
    //encodeEncryModel_key:$("#encodeEncryModel").text().trim().replace(":",""),
    encodeEncryModel_val:$("#encodeEncryModel_val option:selected").val(),

    //输出格式位
    //encodeOutput_key:$("#encodeOutput").text().replace(":",""),
    encodeOutput_val:$("#encodeOutput_val option:selected").val(),

    //加密部分位
    //encodeEncryArea_key:$("#encodeEncryArea").text().trim().replace(":",""),
    encodeEncryArea_val:$("#encodeEncryArea_val option:selected").val(),

    //异常检测编码设计
    //encodeException_key:$("#encodeException").text().trim().replace(":",""),
    encodeException_val:$("#encodeException_val option:selected").val(),

    //完整性校验编码设计
    //encodeComplete_key:$("#encodeComplete").text().trim().replace(":",""),
    encodeComplete_val:$("#encodeComplete_val option:selected").val(),

    //防病毒编码格式设计
    //encodeVirus_key:$("#encodeVirus").text().trim().replace(":",""),
    encodeVirus_val:$("#encodeVirus_val option:selected").val(),

    //协议设计id
    designId:$("#designId").val()
    };


    $.ajax({
        url:HOST.concat("/protocolEncode/id?token=" + config.token),
        type:"PUT",
        data:JSON.stringify(protocolEncodeJson),
        dataType:"json",
        contentType: "application/json",
        success:function (res) {
            if(res.meta.state=="success"){
                console.log(res);
                alert("编码设计保存成功");
                window.location.href="designProtocol.html";
            }else{
                alert("编码设计未更改");
            }
        },
        failed:function () {
            alert("服务器请求失败");
        }
    });



});


